package com.haisen.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.haisen.core.utils.PageResultSet;
import com.haisen.core.utils.WebUtil;
import com.haisen.dataobject.entity.UploadFile;
import com.haisen.dataobject.mapper.base.UploadFileMapper;
import com.haisen.dataobject.query.UploadFileQuery;
import com.haisen.service.UploadFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Service
@Slf4j
public class UploadFileServiceImpl implements UploadFileService {

    @Autowired
    private UploadFileMapper uploadFileMapper;

    @Override
    public void create(UploadFile uploadFile) {
        uploadFileMapper.insertSelective(uploadFile);
    }

    @Override
    public void update(UploadFile uploadFile) {
        uploadFileMapper.updateByPrimaryKeySelective(uploadFile);
    }

    @Override
    public void delete(Long id) {
        uploadFileMapper.deleteByPrimaryKey(id);
    }

  /*  @Override
    public List<UploadFile> find(String filename) {
        Weekend<UploadFile> example = Weekend.of(UploadFile.class);
        WeekendCriteria<UploadFile,Object> criteria = example.weekendCriteria();
        criteria.andLike(UploadFile :: getSourceName,'%'+filename+'%');
        return uploadFileMapper.selectByExample(example);
    }*/

    @Override
    public PageResultSet<UploadFile> findByPage(UploadFileQuery uploadFileQuery) {

        if (!StringUtils.isEmpty(uploadFileQuery.getOrderBy())) {
            PageHelper.orderBy(uploadFileQuery.getOrderBy());
        }

        Weekend<UploadFile> example = Weekend.of(UploadFile.class);
        WeekendCriteria<UploadFile, Object> criteria = example.weekendCriteria();

        if (!StringUtils.isEmpty(uploadFileQuery.getSourceName())) {
            criteria.andLike(UploadFile::getSourceName, "%" + uploadFileQuery.getSourceName() + "%");
        }
        PageHelper.offsetPage(uploadFileQuery.getOffset(), uploadFileQuery.getLimit());
        long total = uploadFileMapper.selectCountByExample(example);
        PageResultSet<UploadFile> resultSet = new PageResultSet<>();
        resultSet.setRows(uploadFileMapper.selectByExample(example));
        resultSet.setTotal(total);
        return resultSet;

    }


    @Override
    public void create(String mapJson) {

        // 获取相关参数
        WebUtil wu = WebUtil.getInstance();
        // 请求对象
        HttpServletRequest req = wu.getRequest();
        // 当前用户
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        // IP地址
        String ip = wu.getIpAddress();

        Map<String, JSONObject> map = (Map) JSON.parse(mapJson);
        for (Map.Entry<String, JSONObject> entry : map.entrySet()) {
            JSONObject jsonObject = entry.getValue();
            String sourceName = jsonObject.getString("sourceName");
            String fileType = jsonObject.getString("sourceType");
            Integer fileSize = Integer.valueOf(jsonObject.getString("sourceSize"));
            String tagPathFile = jsonObject.getString("tagPathFile");
            String tagUrlPath = jsonObject.getString("tagUrlPath");
            UploadFile uploadFile = new UploadFile(username, ip, sourceName, fileType, fileSize, tagPathFile, tagUrlPath);
            uploadFileMapper.insertSelective(uploadFile);

        }

    }
}